#include "mydatebase.h"
#include <QSqlQuery>
#include <QVariant>
#include <QDebug>
#include <QSqlError>
#include <QFile>
#include "logmsg.h"
#include <QCryptographicHash>
MyDatebase::MyDatebase(QObject *parent) :
    QObject(parent)
{
    m_DBName = "./QtNet.dll";
}

bool MyDatebase::InitDB()
{

    QFile file(m_DBName);
    if(!file.exists())
    {
        //qDebug()<<tr("数据库:%1不存在,进行自动创建").arg(m_DBName);
        if(!file.open(QIODevice::ReadWrite))
        {
            //qDebug()<<tr("数据库:%1创建失败").arg(m_DBName);
            getLogMsg()->iLogMsg(tr("数据库:%1创建失败").arg(m_DBName));
            return false;
        }
        else
        {
            file.close();
            m_SqlDB = QSqlDatabase::addDatabase("QSQLITE","UserDB");
            m_SqlDB.setDatabaseName(m_DBName);
            if(!m_SqlDB.open())
            {
                //qDebug()<<"数据库打开失败："<<m_SqlDB.lastError().text();
                getLogMsg()->iLogMsg(tr("数据库打开失败:%1").arg(m_SqlDB.lastError().text()));
                return false;
            }
            if(!createTable(m_SqlDB))
            {
                return false;
            }
        }
    }
    else
    {
        m_SqlDB = QSqlDatabase::addDatabase("QSQLITE","UserDB");
        m_SqlDB.setDatabaseName(m_DBName);
        if(!m_SqlDB.open())
        {
            qDebug()<<"数据库打开失败："<<m_SqlDB.lastError().text();
            getLogMsg()->iLogMsg(tr("数据库打开失败:%1").arg(m_SqlDB.lastError().text()));
            return false;
        }
    }
}

void MyDatebase::CloseDB()
{
    m_SqlDB.close();
}

int MyDatebase::GetUserCount()
{
    QSqlQuery query(m_SqlDB);
    bool ret = query.exec("select count(*) from T_USER");
    while(query.next())
    {
        return query.value(0).toInt();
    }
    return 0;
}

bool MyDatebase::CheckUser(QString UserNo, QString UserKey)
{
    QSqlQuery query(m_SqlDB);
    QByteArray bb;
    bb = QCryptographicHash::hash(UserKey.toAscii(),QCryptographicHash::Md5);
    query.exec(tr("select count(*) from T_USER where UserNO = '%1' and UserKey = '%2'").arg(UserNo).arg(bb.data()));
    while(query.next())
    {
        if(query.value(0).toInt() >= 1)
        {
            return true;
        }
    }
    return false;
}

bool MyDatebase::CreateUser(QString UserNo, QString UserKey)
{
    QSqlQuery query(m_SqlDB);
    query.exec(tr("select count(*) from T_USER where UserNO = '%1'").arg(UserNo));
    while(query.next())
    {
        if(query.value(0).toInt() >= 1)
        {
            return false;
        }
    }
    QByteArray bb;
    bb = QCryptographicHash::hash(UserKey.toAscii(),QCryptographicHash::Md5);
    if(query.exec(tr("insert into T_USER(UserNo,UserKey) values ('%1','%2')").arg(UserNo).arg(bb.data())))
    {
        return true;
    }
    return false;
}

bool MyDatebase::createTable(QSqlDatabase db)
{
    QSqlQuery query(m_SqlDB);
    bool ret = query.exec(CREATE_USER_TABLE);
    if(!ret)
    {
        //qDebug()<<"创建表失败"<<query.lastError();
        getLogMsg()->iLogMsg(tr("创建管理员用户表失败:").arg(query.lastError().text()));
        return false;
    }
    //    ret = query.exec("insert into T_USER(UserNo,UserKey) values ('autoveh','autoveh')");
    //    if(!ret)
    //    {
    //        qDebug()<<"创建原始管理员失败"<<query.lastError();
    //        getLogMsg()->iLogMsg(tr("创建管理员账号失败").arg(query.lastError().text()));
    //        return false;
    //    }
    return true;
}
MyDatebase* getDB()
{
    static MyDatebase* db = NULL;
    if(db == NULL)
    {
        db = new MyDatebase();
    }
    return db;
}
